package com.clannad.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.clannad.commonutils.JwtUtils;
import com.clannad.commonutils.R;
import com.clannad.eduorder.entity.Order;
import com.clannad.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author clannad
 * @since 2022-08-22
 */
@RestController
@RequestMapping("/eduorder/order")
//@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        String orderNo=orderService.createOrders(courseId,memberIdByJwtToken);
        return R.ok().data("orderId",orderNo);
    }


    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        LambdaQueryWrapper<Order> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getOrderNo,orderId);
        Order one = orderService.getOne(lambdaQueryWrapper);
        return R.ok().data("item",one);
    }

    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
        LambdaQueryWrapper<Order> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getCourseId,courseId);
        lambdaQueryWrapper.eq(Order::getMemberId,memberId);
        lambdaQueryWrapper.eq(Order::getStatus,1);
        int count= orderService.count(lambdaQueryWrapper);
        if(count>0){
            return true;
        }
        return false;
    }
}

